package com.talkology.pilot.start.shiro.service.impl;

import com.talkology.pilot.database.dao.SysPermissionMapper;
import com.talkology.pilot.database.dao.SysUserMapper;
import com.talkology.pilot.database.model.SysPermission;
import com.talkology.pilot.database.model.SysUser;
import com.talkology.pilot.start.shiro.service.ILoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements ILoginService {
    private static final Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void addSysUser(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        LOG.info("Add new user : {}", sysUser);
    }

    @Override
    public SysUser findByAccount(String account) {
        return sysUserMapper.selectSysUserByAccount(account);
    }

    @Override
    public List<SysPermission> findPermissionByAccount(String account) {
        SysUser sysUser = sysUserMapper.selectSysUserByAccount(account);
        return sysPermissionMapper.selectByRoleId(sysUser.getRoleid());
    }

}
